package cpre556.project2.eclipsePlugin.support;

import java.util.Vector;

/**
 * 
 */
public class Filter{
	public final static int SEARCH_WINDOW = 0;
	
	private Saver saver;
	
	/**
	 * Constructor
	 * @param s Saver instance
	 */
	public Filter(Saver s){
		saver = s;
	}
	
	/**
	 * Return Vulnerabilities
	 * @return
	 */
	public Vector<Vulnerability> getVulnerabilities(){
		Vector<Vulnerability> allVulnerbilities = new Vector<Vulnerability>();
		Vector<IgnoredVulnerability> ignoredVulnerbilities = new Vector<IgnoredVulnerability>();
		Vector<Vulnerability> filteredVulnerbilities = new Vector<Vulnerability>();
		
		XMLtoVulnerabilitiesVector v = new XMLtoVulnerabilitiesVector(saver.getFile());
		XMLIgnoreReader iv = new XMLIgnoreReader(saver);
		
		allVulnerbilities = v.getVulnerabilities();
		ignoredVulnerbilities = iv.getIgnoredVulnerabilitiesVector();
		
		int iCount = 0;
		
		while(iCount < ignoredVulnerbilities.size()){
			boolean found = false;
			int foundPosition = -1;
			
			int iLineNumber = ignoredVulnerbilities.get(iCount).getLineNumber();
			String iCode = ignoredVulnerbilities.get(iCount).getLineOfCode().trim();
			
			int aCount = 0;
			
			while(aCount < allVulnerbilities.size()){
				SourceReader sr = new SourceReader(saver);
				int aLineNumber = allVulnerbilities.get(aCount).getLineNumber();
				String aCode = sr.getLineOfCode(aLineNumber).trim();
				
				if(iCode.equals((Object)aCode) && (iLineNumber == aLineNumber)){
					found = true;
					foundPosition = aCount;
				}
				
				/**
				 * Check the predecessors and successors
				 */
				if(!found && iCode.equals((Object)aCode)){
					IgnoredVulnerability iVuln = ignoredVulnerbilities.get(iCount);
					
					String pred1 = removeXMLSafeCharacters(iVuln.getPredecessor1());
					String pred2 = removeXMLSafeCharacters(iVuln.getPredecessor2());
					String pred3 = removeXMLSafeCharacters(iVuln.getPredecessor3());
					String succ1 = removeXMLSafeCharacters(iVuln.getSuccessor1());
					String succ2 = removeXMLSafeCharacters(iVuln.getSuccessor2());
					String succ3 = removeXMLSafeCharacters(iVuln.getSuccessor3());
					
					if(pred1.equals(sr.getLineOfCode(aLineNumber - 1).trim()) &&
							pred2.equals(sr.getLineOfCode(aLineNumber - 2).trim()) &&
							pred3.equals(sr.getLineOfCode(aLineNumber - 3).trim()) &&
							succ1.equals(sr.getLineOfCode(aLineNumber + 1).trim()) &&
							succ2.equals(sr.getLineOfCode(aLineNumber + 2).trim()) &&
							succ3.equals(sr.getLineOfCode(aLineNumber + 3).trim())){
						iVuln.setLineNumber(aLineNumber);
						System.out.println(aLineNumber);
						ignoredVulnerbilities.set(iCount, iVuln);
					}
					
					found = true;
					foundPosition = aCount;
				}
				
				if(!found){
					for(int i = 1; i <= SEARCH_WINDOW; i++){						
						iCode = sr.getLineOfCode(iLineNumber - i).trim();
						
						if(iCode.equals((Object)aCode)){
							found = true;
							ignoredVulnerbilities.get(iCount).setLineNumber(iLineNumber - i);
							foundPosition = aCount;
						}
					}
				}
				
				if(!found){
					for(int i = 1; i <= SEARCH_WINDOW; i++){
						iCode = sr.getLineOfCode(iLineNumber + i).trim();
						
						if(iCode.equals((Object)aCode)){
							found = true;
							ignoredVulnerbilities.get(iCount).setLineNumber(iLineNumber + i);
							foundPosition = aCount;
						}
					}								
				}
				
				if(!found)
					aCount++;
				else
					break;
			}
			
			if(!found){
				ignoredVulnerbilities.remove(iCount);
			}	
			else{
				allVulnerbilities.remove(foundPosition);
			}
			
			found = false;
			foundPosition = -1;
			iCount++;
		}
		
		int newSize = ignoredVulnerbilities.size();
		
		XMLIgnoreWriter xmlIgnoreWriter = new XMLIgnoreWriter(saver);
		xmlIgnoreWriter.writeBlankXML();
		
		if(newSize > 0){		
			for(int p = 0; p < newSize; p++){
				Vulnerability nextIgnoredVulnerbility = ignoredVulnerbilities.get(p);
										
				xmlIgnoreWriter.writeXML(nextIgnoredVulnerbility);				
			}
		}
		
		filteredVulnerbilities = allVulnerbilities; 
		
		return filteredVulnerbilities;		
	}
	
	public String removeXMLSafeCharacters(String str){
		while(str.indexOf("&amp;") != -1 || str.indexOf("&lt;") != -1 || str.indexOf("&gt;") != -1){
			if(str.indexOf("&amp;") != -1){
				str = str.substring(0, str.indexOf("&amp;")) + "&" + str.substring(str.indexOf("&amp;")+5, str.length());
			}
			if(str.indexOf("&lt;") != -1){
				str = str.substring(0, str.indexOf("&lt;")) + "<" + str.substring(str.indexOf("&lt;") + 4, str.length());
			}
			if(str.indexOf("&gt;") != -1){
				str = str.substring(0,str.indexOf("&gt;"))+">"+str.substring(str.indexOf("&gt;")+4, str.length());
			}
		}
		
		return str;
	}

}
